﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuKou.Shopspace.Services;
using LuKou.Shopspace.Domains;
using LuKou.Shopspace;
using LuKou.Web.BLL.Extensions;

namespace LuKou.Web.BLL.Service
{
    public class GoodsService
    {
        private static IGoodsSvr _svr;
        private static ICategorySvr cateSvr;

        static GoodsService()
        {
            _svr = ServiceHelper.Get<IGoodsSvr>("IGoodsSvr");
            cateSvr = ServiceHelper.Get<ICategorySvr>("ICategorySvr");
        }
        public static GoodsImage GetImage(long id)
        {
            return _svr.GetGoodsImageDetail(id, true);
        }

        public static List<GoodsImage> GetImageList(long id)
        {
            GoodsImageQueryParam param = new GoodsImageQueryParam();
            param.AddParamOfGoodsID(id);
            param.AddParamOfIsMajor(true);
            return _svr.GetGoodsImageList(param, true, 0, 100);
        }

        public static List<Goods> GetGoodsList(bool detail, int skipResults, int maxResults)
        {
            GoodsQueryParam gqp = new GoodsQueryParam();
            return _svr.GetGoodsList(gqp, detail, skipResults, maxResults);
        }

        public static int GetGoodsCount()
        {
            GoodsQueryParam gqp = new GoodsQueryParam();
            return _svr.GetGoodsCount(gqp);
        }

        public static Goods GetGoodsDetail(long id)
        {
            return _svr.GetGoodsDetail(id, true);
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public static long AddGoods(Goods goods)
        {
            //添加商品信息
            long goodsId = _svr.AddGoods(goods);
            return goodsId;
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public static long EditGoods(Goods goods)
        {
            _svr.ModifyGoods(goods);
            return goods.ID;
        }

        public static void Delete(int[] ids)
        {
            GoodsQueryParam param = new GoodsQueryParam();
            for (int i = 0; i < ids.Length; i++)
            {
                param.AddParamOfID((long)ids[i]);
            }
            _svr.DeleteGoods(param);
        }

        /// <summary>
        /// 添加商品图片
        /// </summary>
        /// <param name="goodsId">所属商品ID</param>
        /// <param name="image">图片URL</param>
        /// <param name="isMajor">是否主图片，主图片在商品基本信息中显示，每个商品限5个，非主图片在详细信息中显示，限10个</param>
        public static void AddGoodsImage(long goodsId, string image, bool isMajor)
        {
            _svr.AddGoodsImage(new GoodsImage() { GoodsID = goodsId, ImgURL = image, IsMajor = isMajor });
        }

        /// <summary>
        /// 删除商品图片
        /// </summary>
        /// <param name="goodsId">所属商品ID</param>
        /// <param name="isMajor">是否主图片</param>
        public static void DeleteGoodsImage(long goodsId, bool isMajor)
        {
            GoodsImageQueryParam param = new GoodsImageQueryParam();
            param.AddParamOfIsMajor(isMajor);
            param.AddParamOfGoodsID(goodsId);
            _svr.DeleteGoodsImage(param);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PagedList<GoodsAccessRecord> GetGoodsAccessRecord(int index, int pageSize)
        {
            return GetGoodsAccessRecord(index, pageSize, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderFields"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static PagedList<GoodsAccessRecord> GetGoodsAccessRecord(int index, int pageSize, IList<String> orderFields)
        {
            return GetGoodsAccessRecord(null, index, pageSize, orderFields);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accessType"></param>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderFields"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static PagedList<GoodsAccessRecord> GetGoodsAccessRecord(IList<EGoodsAccessType> accessType, int index, int pageSize, IList<String> orderFields)
        {
            GoodsAccessRecordQueryParam param = new GoodsAccessRecordQueryParam();
            if (accessType != null && accessType.Count > 0)
            {
                for (int i = 0; i < accessType.Count; i++)
                {
                    param.AddParamOfGoodsAccessType((int)accessType[i]);
                }
            }
            if (orderFields != null && orderFields.Count > 0)
            {
                foreach (var orderField in orderFields)
                {
                    param.SetOrderByField(orderField);
                }
            }
            if (index < 1) index = 1;
            if (pageSize < 1) pageSize = 10;
            return new PagedList<GoodsAccessRecord>(_svr.GetGoodsAccessRecordList(param, true, pageSize * (index - 1), pageSize), index, pageSize, _svr.GetGoodsAccessRecordCount(param));
        }
    }
}
